package com.buer.train.api;

import com.buer.train.business.domain.DocDO;
import com.buer.train.business.domain.TeacherDO;
import com.buer.train.business.dto.DocDTO;
import com.buer.train.business.service.DocService;
import com.buer.train.business.service.EsService;
import com.buer.train.business.service.TeacherService;
import com.buer.train.common.annotation.Auth;
import com.buer.train.common.eumn.ResultEnum;
import com.buer.train.common.holder.LoginUser;
import com.buer.train.common.holder.LoginUserHolder;
import com.buer.train.common.utils.BaseResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/doc")
@Api(tags = {"文章相关接口"})
public class DocController {

    @Autowired
    private DocService docService;

    @Autowired
    private TeacherService teacherService;


    @Autowired
    private EsService esService;

    @RequestMapping(value = "/doc/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("文章列表")
    @Auth(value = "doc:doc:list")
    public BaseResult docList(@RequestBody Map<String, Object> params) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        params.put("depts",loginUserHolder.getNodeDepts());

        PageInfo<DocDO> docList = docService.getDocList(params);
        Map map = new HashMap<>();
        map.put("list", docList.getList());
        map.put("total", docList.getTotal());
        return BaseResult.build(ResultEnum.SUCCESS, map);
    }

    @RequestMapping(value = "/doc/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("文档添加")
    @Auth(value = "doc:doc:add")
    @Transactional
    public BaseResult docAdd(@RequestBody DocDO docDO) {

        docDO.setCreateTime(new Date());
        docDO.setUpdateTime(new Date());

        TeacherDO teacherDO = teacherService.get(docDO.getAuthorId());
        docDO.setAuthor(teacherDO.getName());

        if(docService.save(docDO)>0){
            DocDTO docDTO=new DocDTO();
            docDTO.setId(docDO.getId());
            docDTO.setAuthor(docDO.getAuthor());
            docDTO.setContent(docDO.getContent());
            docDTO.setTitle(docDO.getTitle());
            docDTO.setCreateTime(new Date());
            docDTO.setUpdateTime(new Date());
            int add = esService.add(docDTO);

            if(add>0){
                return BaseResult.build(ResultEnum.SUCCESS);
            }


        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/doc/remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID删除文档")
    @Auth(value = "doc:doc:remove")
    @Transactional
    public BaseResult docRemove(@RequestBody Map<String, Object> params) {
        if (Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.FAILURE);
        }
        Integer id = Integer.valueOf(params.get("id") + "");

        if(docService.remove(id)>0){

            int delete = esService.delete(id);

            if(delete>0){
                return BaseResult.build(ResultEnum.SUCCESS);
            }
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/doc/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("修改文档")
    @Auth(value = "doc:doc:update")
    @Transactional
    public BaseResult docUpdate(@RequestBody DocDO docDO) {

        TeacherDO teacherDO = teacherService.get(docDO.getAuthorId());
        docDO.setAuthor(teacherDO.getName());
        docDO.setUpdateTime(new Date());

        if(docService.update(docDO)>0){

            DocDTO docDTO=new DocDTO();
            docDTO.setId(docDO.getId());
            docDTO.setAuthor(docDO.getAuthor());
            docDTO.setContent(docDO.getContent());
            docDTO.setTitle(docDO.getTitle());
            docDTO.setCreateTime(new Date());
            docDTO.setUpdateTime(new Date());
            int update = esService.update(docDTO);

            if(update>0){
                return BaseResult.build(ResultEnum.SUCCESS);
            }
        }
        return BaseResult.build(ResultEnum.FAILURE);

    }

    @RequestMapping(value = "/doc/get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID查询文章")
    @Auth
    public BaseResult docGet(@RequestBody Map<String, Object> params) {
        if(Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        DocDO docDO = docService.get(id);

        return BaseResult.build(ResultEnum.SUCCESS,docDO);
    }


    @PostMapping("/doc/addEs")
    @ApiOperation("测试-添加测试数据")
    public BaseResult addEs(@RequestBody DocDTO docDTO) throws IOException {
        int add = esService.add(docDTO);
        return BaseResult.build(ResultEnum.SUCCESS,add);
    }

    @PostMapping("/doc/updateEs")
    @ApiOperation("测试-更新测试数据")
    public BaseResult updateEs(@RequestBody DocDTO docDTO) throws IOException {
        int update = esService.update(docDTO);
        return BaseResult.build(ResultEnum.SUCCESS,update);
    }

    @PostMapping("/doc/deleteEs")
    @ApiOperation("测试-删除测试数据")
    public BaseResult deleteEs(int id) throws IOException {
        int delete = esService.delete(id);
        return BaseResult.build(ResultEnum.SUCCESS,delete);
    }


    @PostMapping("/doc/searchByPage")
    @ApiOperation("测试-获取分页测试数据")
    public BaseResult searchByPage(String keywords, int pageNum, int pageSize) throws IOException {
        Map<String, Object> map = esService.searchByPage(keywords, pageNum, pageSize);
        return BaseResult.build(ResultEnum.SUCCESS,map);
    }

}